package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import org.osmf.utils.OSMFSettings;
	
	import org.osmf.containers.MediaContainer;
	import org.osmf.events.MediaPlayerStateChangeEvent;
	import org.osmf.layout.HorizontalAlign;
	import org.osmf.layout.ScaleMode;
	import org.osmf.layout.VerticalAlign;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaPlayerState;
	
	import qbrick.professional.common.event.QbrickStatusEvent;
	import qbrick.professional.v4.api.mediaplayer.IQbrickProMediaPlayer;
	import qbrick.professional.v4.api.mediaplayer.QbrickProMediaPlayer;
	import qbrick.professional.v4.api.octopus.IQbrickProOctopus;
	import qbrick.professional.v4.api.octopus.QbrickProOctopus;
	
	import qbrick.professional.v4.api.octopus.model.vo.media.item.IMedia;
	import qbrick.professional.v4.api.octopus.model.vo.media.item.IStream;
	
	/**
	 * ...
	 * @author Henrik Riise Hansen
	 */
	
	public class Shell extends Sprite 
	{
		private var _octopus : IQbrickProOctopus;
		private var _mediaPlayer : IQbrickProMediaPlayer;
		private var _video : MediaContainer;
		
		public function Shell():void 
		{
			trace('init');
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init():void
		{
			trace('loaderinfo: ' + loaderInfo);
			
			// Initiate the octopus
			_octopus = new QbrickProOctopus( loaderInfo );
			_octopus.addEventListener( QbrickStatusEvent.LOAD_COMPLETE, onLoadComplete );
			
			// Initiate the media player 
			_mediaPlayer = new QbrickProMediaPlayer();
			_mediaPlayer.mediaPlayer.addEventListener( MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE, onStateChange, false, 0, true );
			
			// Initiate the media container
			_video = new MediaContainer();
			_video.layoutMetadata.scaleMode = ScaleMode.NONE;
			_video.layoutMetadata.width = 600;
			_video.layoutMetadata.height = 400;
			_video.layoutMetadata.percentHeight = 100;
			_video.layoutMetadata.percentWidth = 100;
			_video.layoutMetadata.horizontalAlign = HorizontalAlign.CENTER;
			_video.layoutMetadata.verticalAlign = VerticalAlign.MIDDLE;
			_video.backgroundColor = 0x000000;
			_video.backgroundAlpha = 1;
			_video.clipChildren = true;
			addChild( _video );
			
			// Load the data from Qbrick
			_octopus.load();
		}
		
		private function onLoadComplete( event:QbrickStatusEvent ):void
		{
			trace('load complete');
			
			// Search through all the IMedia items and play the first video
			// stream found
			var items : Vector.<IMedia> = _octopus.mediaItems;
			var itemsLen : int = items.length;
			if( itemsLen > 0 )
			{
				var stream : IStream = items[0].getMainStream();
				var mediaElement : MediaElement = _mediaPlayer.createMediaElement( stream );
				
				_video.addMediaElement( mediaElement );
				_mediaPlayer.mediaPlayer.media = mediaElement;
			}
		}
		
		private function onStateChange( event:MediaPlayerStateChangeEvent ):void
		{
			// Listen for the ready event from OSMF. If the media is playable,
			// then play the video stream
			trace('state: ' + event.state);
			
			if( event.state == MediaPlayerState.READY )
			{
				if( _mediaPlayer.mediaPlayer.canPlay )
				{
					_mediaPlayer.mediaPlayer.play();
				}
			}
		}
	}
}